from django.test import TestCase

from mdcssport.mdcsapp import elo_utils 
from mdcsapp.result import Result

from django.core.exceptions import ValidationError
"""
Testira parsiranje i validaciju rezultata
"""
class ResultValidityTestCase(TestCase):
    
    def setUp(self):
        pass

    def runTest(self):
        self.testParseResults()
        self.testChess()
        self.testTableFootball()
        self.testBasket()

    def testParseResults(self):
        r = Result.parse_result("1:0")
        self.assertEqual(r.home_result, 1)
        self.assertEqual(r.away_result, 0)
        self.assertEqual(r.winner, 1)

        r = Result.parse_result("1:00")
        self.assertEqual(r.home_result, 1)
        self.assertEqual(r.away_result, 0)
        self.assertEqual(r.winner, 1)

        r = Result.parse_result("3:21")
        self.assertEqual(r.home_result, 3)
        self.assertEqual(r.away_result, 21)
        self.assertEqual(r.winner, 3)

        r = Result.parse_result("5:5")
        self.assertEqual(r.home_result, 5)
        self.assertEqual(r.away_result, 5)
        self.assertEqual(r.winner, 2)


    def testChess(self):
        r = Result.parse_result("1:0")
        r.is_valid(1)

        r = Result.parse_result("0:0")
        r.is_valid(1)
        
        r = Result.parse_result("0:1")
        r.is_valid(1)
        
        r = Result.parse_result("2:0")
        with self.assertRaises(ValidationError):
            r.is_valid(1)

        r = Result.parse_result("0:2")
        with self.assertRaises(ValidationError):
            r.is_valid(1)

        r = Result.parse_result("1:1")
        with self.assertRaises(ValidationError):
            r.is_valid(1)

    def testTableFootball(self):
        r = Result.parse_result("5:3")
        r.is_valid(2)

        r = Result.parse_result("4:5")
        r.is_valid(2)

        r = Result.parse_result("1:5")
        r.is_valid(2)
        
        r = Result.parse_result("4:6")
        r.is_valid(2)

        r = Result.parse_result("12:14")
        r.is_valid(2)
        
        r = Result.parse_result("0:0")
        with self.assertRaises(ValidationError):
            r.is_valid(2)

        r = Result.parse_result("5:5")
        with self.assertRaises(ValidationError):
            r.is_valid(2)

        r = Result.parse_result("4:0")
        with self.assertRaises(ValidationError):
            r.is_valid(2)

        r = Result.parse_result("7:6")
        with self.assertRaises(ValidationError):
            r.is_valid(2)

        r = Result.parse_result("13:16")
        with self.assertRaises(ValidationError):
            r.is_valid(2)

    def testBasket(self):
        r = Result.parse_result("11:3")
        r.is_valid(4)

        r = Result.parse_result("5:11")
        r.is_valid(4)

        r = Result.parse_result("11:9")
        r.is_valid(4)

        r = Result.parse_result("9:11")
        r.is_valid(4)

        r = Result.parse_result("21:14")
        r.is_valid(4)

        r = Result.parse_result("21:19")
        r.is_valid(4)

        r = Result.parse_result("15:21")
        r.is_valid(4)

        r = Result.parse_result("19:21")
        r.is_valid(4)

        r = Result.parse_result("20:22")
        r.is_valid(4)

        r = Result.parse_result("12:10")
        r.is_valid(4)

        r = Result.parse_result("10:12")
        r.is_valid(4)

        r = Result.parse_result("15:13")
        r.is_valid(4)

        r = Result.parse_result("26:28")
        r.is_valid(4)

        r = Result.parse_result("0:0")
        with self.assertRaises(ValidationError):
            r.is_valid(4)

        r = Result.parse_result("6:3")
        with self.assertRaises(ValidationError):
            r.is_valid(4)

        r = Result.parse_result("6:4")
        with self.assertRaises(ValidationError):
            r.is_valid(4)

        r = Result.parse_result("11:11")
        with self.assertRaises(ValidationError):
            r.is_valid(4)

        r = Result.parse_result("11:10")
        with self.assertRaises(ValidationError):
            r.is_valid(4)

        r = Result.parse_result("15:12")
        with self.assertRaises(ValidationError):
            r.is_valid(4)

        r = Result.parse_result("12:20")
        with self.assertRaises(ValidationError):
            r.is_valid(4)

        r = Result.parse_result("20:21")
        with self.assertRaises(ValidationError):
            r.is_valid(4)

        r = Result.parse_result("20:23")
        with self.assertRaises(ValidationError):
            r.is_valid(4)